package com.itheima.info.manger.dao;

import com.itheima.info.manger.domain.Student;

import java.util.ArrayList;

public class OtherStudentDao {
    //创建Arraylist集合接收数据
    private static ArrayList<Student> student = new ArrayList<>();

    public boolean addStudent(Student stu) {
       student.add(stu);
       return true;
    }

    //创建findAllStudent()方法,将Student[]数组返回.
    public Student[] findAllStudent() {
        Student[] student1 = new Student[student.size()];
        for (int i = 0; i < student1.length; i++) {
            student1[i] = student.get(i);
        }
        return student1;
    }

    public boolean deleteStudent(String id) {
        //遍历数组中元素,判断传递来的id是否与数组中的id相同,如果相同将id对应索引下的元素变为null,并返回true;否则返回false.
        for (int i = 0; i < student.size(); i++) {
            if (student.get(i).getId().equals(id)){
                student.remove(i);
                return true;
            }
        }
        return false;
    }

    public boolean updateStudent(Student students) {
        //遍历数组中的元素,判断传递来的students数组id是否与student数组中的id相同,如果相同将students赋值给对应索引的student,并返回true;否则返回false.
        for (int i = 0; i < student.size(); i++) {
            if (student.get(i).getId() != null && student.get(i).getId().equals(students.getId())){
                student.set(i,students);
                return true;
            }
        }
        return false;
    }
}
